﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Lister;

namespace COFE.Core.Profile
{
    /// <summary>
    /// Automatically register all volumes in Volume registrar.
    /// </summary>
    public class AllVolumeManager : IVolumeManager
    {
        #region Constructor

        public AllVolumeManager(IUserProfile userProfile)
        {
            UserProfile = userProfile;
        }

        #endregion

        #region Methods

        public void RegisterVolume(string listerKey)
        {
            //Do nothing.
        }

        private IDirectoryInfo[] getRootDirectories()
        {
            //return (from volinfo in COFEShared.VolumeRegistrar.Volumes
            //        where volinfo.Value.IsPrimaryDirectoryLister()
            //        select volinfo.Value)
            //        .SelectMany( vol => (vol.DirectoryLister as IPrimaryDirectoryLister).RootDirectories)
            //   .ToArray();

            List<IDirectoryInfo> retList = new List<IDirectoryInfo>();

            foreach (var volinfo in COFEShared.VolumeRegistrar.Volumes)
                if (volinfo.Value.IsPrimaryDirectoryLister())
                    foreach (var dir in (volinfo.Value.DirectoryLister as IPrimaryDirectoryLister).RootDirectories)
                        retList.Add(dir);

            return retList.ToArray();
        }

        private List<IVolume> getPrimaryVolumes()
        {
            //COFEShared.VolumeRegistrar.Volumes is Dictionary<string, IVolume>
            return (from volinfo in COFEShared.VolumeRegistrar.Volumes
                    where volinfo.Value.IsPrimaryDirectoryLister()
                    select volinfo.Value)
                .ToList();

        }

        private List<IVolume> getSecondaryVolumes()
        {
            //COFEShared.VolumeRegistrar.Volumes is Dictionary<string, IVolume>
            return (from volinfo in COFEShared.VolumeRegistrar.Volumes
                    where volinfo.Value.IsSecondaryDirectoryLister()
                    select volinfo.Value).ToList();
        }

        private ITempManager getTempManager()
        {
            if (_tempManager is NullTempManager)
            {
                foreach (var vol in PrimaryVolumes)
                {
                    IPrimaryDirectoryLister primaryLister = vol.DirectoryLister as IPrimaryDirectoryLister;
                    if (!String.IsNullOrEmpty(primaryLister.RootTempPath))
                        _tempManager = new DefaultTempManager(primaryLister.RootTempPath, COFEShared.CurrentUserProfile.UserName);
                }
            }
            return _tempManager;
        }


        #endregion

        #region Data
        
        ITempManager _tempManager = new NullTempManager();

        #endregion

        #region Public Properties

        public IVolume this[string listerKey, bool lookupPrimary = false, bool lookupSecondary = false]
        {
            get
            {
                if (lookupPrimary)
                    foreach (var vol in getPrimaryVolumes()) if (vol.ListerKey == listerKey) return vol;
                if (lookupSecondary)
                    foreach (var vol in getSecondaryVolumes()) if (vol.ListerKey == listerKey) return vol;
                return null;
            }
        }

        public IUserProfile UserProfile { get; set; }
        public ITempManager TempManager { get { return getTempManager(); } private set { _tempManager = value; } }
        public IDirectoryInfo[] RootDirectories { get { return getRootDirectories(); } }
        public List<IVolume> PrimaryVolumes { get { return getPrimaryVolumes(); } }
        public List<IVolume> SecondaryVolumes { get { return getSecondaryVolumes(); } }

        #endregion

    }
}
